package com.slipper.system.codeless.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.slipper.util.Tool;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;

import com.slipper.system.codeless.dao.PageParamDao;
import com.slipper.system.codeless.bo.PageParamBO;

import com.slipper.core.transaction.ReadTransactional;
import com.slipper.core.transaction.WriteTransactional;
import com.slipper.util.UUID;

@Service
@ReadTransactional
public class PageParamService {
    @Resource
    private PageParamDao pageParamDao;
    
    public PageParamBO queryBy(String id) {
        return pageParamDao.queryBy(id);
    }
    
    public List<PageParamBO> queryList(PageParamBO bo) {
        return pageParamDao.queryList(bo);
    }
    
    public PageInfo<PageParamBO> queryPage(PageParamBO bo) {
        PageHelper.startPage(bo.getPageNumber(), bo.getPageSize());
        return new PageInfo<>(pageParamDao.queryList(bo));
    }
    
    @WriteTransactional
    public int save(PageParamBO bo) {
        if (Tool.isNotBlank(bo.getId())) {
            bo.setUpdateTime(System.currentTimeMillis());
            return pageParamDao.update(bo);
        } else {
            bo.setId(UUID.getUUID());
            bo.setUpdateTime(System.currentTimeMillis());
            bo.setCreateTime(System.currentTimeMillis());
            return pageParamDao.insert(bo);
        }
    }

    @WriteTransactional
    public void saveList(List<PageParamBO> list) {
        if (list == null) return;

        list.forEach(item -> {
            if (pageParamDao.queryBy(item.getId()) != null) {
                item.setUpdateTime(System.currentTimeMillis());
                pageParamDao.update(item);
            } else {
                item.setUpdateTime(System.currentTimeMillis());
                item.setCreateTime(System.currentTimeMillis());
                pageParamDao.insert(item);
            }
        });
    }

    @WriteTransactional
    public int save(String pageId, List<PageParamBO> list, String employeeId) {
        if (Tool.isBlank(pageId)) {
            return 0;
        }

        PageParamBO qo = new PageParamBO();
        qo.setPageId(pageId);
        List<PageParamBO> paramList = pageParamDao.queryList(qo);

        for (int i=0;i<list.size(); i++) {
            PageParamBO param = list.get(i);
            param.setPageId(pageId);
            param.setOrdinal(i);

            if (paramList.stream().anyMatch(comp -> comp.getId().equals(param.getId()))) {
                param.setUpdateId(employeeId);
                param.setUpdateTime(System.currentTimeMillis());
                pageParamDao.update(param);
            } else {
                param.setUpdateId(employeeId);
                param.setCreateId(employeeId);
                param.setUpdateTime(System.currentTimeMillis());
                param.setCreateTime(System.currentTimeMillis());
                pageParamDao.insert(param);
            }
        }

        // 删除被删除的数据
        paramList.stream()
                .filter(comp -> list.stream().noneMatch(temp -> comp.getId().equals(temp.getId())))
                .forEach(comp -> pageParamDao.delete(comp.getId()));

        return 1;
    }
    
    @WriteTransactional
    public int delete(PageParamBO bo) {
        return pageParamDao.delete(bo.getId());
    }

    public List<PageParamBO> queryByPageId(String pageId) {
        PageParamBO qo = new PageParamBO();
        qo.setPageId(pageId);
        return pageParamDao.queryList(qo);
    }

    @WriteTransactional
    public void deleteByPageId(String pageId) {
        pageParamDao.deleteByPageId(pageId);
    }

    @WriteTransactional
    public Map<String, String> copyByPageId(String oldPageId, String newPageId, String employeeId) {
        Map<String, String> oldMap = new HashMap<>();
        if (Tool.isBlank(oldPageId)) {
            return oldMap;
        }


        PageParamBO qo = new PageParamBO();
        qo.setPageId(oldPageId);
        pageParamDao.queryList(qo).forEach(param -> {
            String oldParamId = param.getId();
            param.setId(UUID.getUUID());
            param.setPageId(newPageId);
            param.setUpdateId(employeeId);
            param.setCreateId(employeeId);
            param.setUpdateTime(System.currentTimeMillis());
            param.setCreateTime(System.currentTimeMillis());
            pageParamDao.insert(param);
            oldMap.put(oldParamId, param.getId());
        });
        return oldMap;
    }
}
